package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.User;
import com.imooc.mall.service.UserService;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/test")
    @ResponseBody
    public User getUser(){
        return userService.getUser();
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse register(@RequestParam("username") String username,
                                    @RequestParam("password") String password)
            throws ImoocMallException {

        if (org.springframework.util.StringUtils.isEmpty(username)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (org.springframework.util.StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        //密码长度不能小于八位
        if (password.length()<8){
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        //实现添加方法
        userService.register(username,password);
        return ApiRestResponse.success();
    }


    /**
     * 用户登录功能
     */
    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestParam("username") String username
            , @RequestParam("password") String password,
                                 HttpSession session){
      //先判断账号密码是否符合要求
        if (org.springframework.util.StringUtils.isEmpty(username)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (org.springframework.util.StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        //密码长度不能小于八位
        if (password.length()<8){
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        //然后调用登录程序
        User user = userService.login(username, password);
        user.setPassword(null);
        session.setAttribute(Constant.IMOOC_MALL_USER,user);
        return ApiRestResponse.success(user);
    }

    /**
     * 修改用户个性签名
     */

    @PostMapping("/user/update")
    @ResponseBody
    public ApiRestResponse update(HttpSession session,
                                  @RequestParam("signature") String signature){
        //现从session获取用户信息
        User currentUser = (User)session.getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser==null){
            //用户未登录，提示登录
            throw new ImoocMallException(ImoocMallExceptionEnum.NEED_LOGION);
        }
        User user = new User();
        //设置其签名
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userService.updateSignature(user);
        return ApiRestResponse.success();

    }

    /**
     * 退出登录系统
     */
    @PostMapping("/user/logout")
    @ResponseBody
    public ApiRestResponse logout(HttpSession session){
        //删除session对应的key
        session.removeAttribute(Constant.IMOOC_MALL_USER);
        return ApiRestResponse.success();

    }
    /**
     * 管理员登录
     */
    @PostMapping("/admin/login")
    @ResponseBody
    public ApiRestResponse adminlogin(@RequestParam("username") String username
            , @RequestParam("password") String password,
                                 HttpSession session){
        //先判断账号密码是否符合要求
        if (org.springframework.util.StringUtils.isEmpty(username)){
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_USER_NAME);
        }
        if (org.springframework.util.StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_PASSWORD);
        }
        //密码长度不能小于八位
        if (password.length()<8){
            return ApiRestResponse.error(ImoocMallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        //然后调用管理员的登录程序
        User user = userService.login(username, password);
        if (userService.checkAdminRole(user)) {
            user.setPassword(null);
            session.setAttribute(Constant.IMOOC_MALL_USER,user);
            return ApiRestResponse.success(user);
        }else {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
        }

    }

}
